﻿using Mapster;
using NP.BPMReportPlatform.BaseData.Dto;
using NP.BPMReportPlatform.DBHelper;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.Entities.BPM_Platform;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using static NP.BPMReportPlatform.Contract.Dtos.ContractEnum;

namespace NP.BPMReportPlatform.BaseData
{
    /// <summary>
    /// 基础数据接口服务
    /// </summary>
    public class BaseDataAppService : BPMReportPlatformAppService, IBaseDataAppService
    {
        private readonly SqlSugarClient _db;
        readonly SqlSugarClient _dbPlatform;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sqlSugarAppService"></param>
        public BaseDataAppService(ISqlSugarAppService sqlSugarAppService)
        {
            _db = sqlSugarAppService.GetBusinessInstance();
            _dbPlatform = sqlSugarAppService.GetPlatformInstance();
        }

        /// <summary>
        /// 获取字典表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetKeyValuesOutput>> GetKeyValues(GetKeyValuesInput input)
        {
            RefAsync<int> totalCount = 0;
            input.MaxResultCount = 200;

            var page = await _db.Queryable<KeyValues>()
                .WhereIF(!input.Code.IsNullOrWhiteSpace(), x => x.Code.Equals(input.Code))
                .WhereIF(!input.Name.IsNullOrWhiteSpace(), x => x.Name.Contains(input.Name))
                .WhereIF(!input.ParentCode.IsNullOrWhiteSpace(), x => x.ParentCode.Equals(input.ParentCode))
                .WhereIF(!input.CatalogCode.IsNullOrWhiteSpace(), x => x.CatalogCode.Equals(input.CatalogCode))
                .OrderBy(!input.Sorting.IsNullOrWhiteSpace() ? input.Sorting : "Sort")
                .ToPageListAsync(input.SkipCount * input.MaxResultCount, input.MaxResultCount, totalCount);

            var listDto = ObjectMapper.Map<List<KeyValues>, List<GetKeyValuesOutput>>(page);

            // 返回结果
            return new PagedResultDto<GetKeyValuesOutput>()
            {
                TotalCount = totalCount,
                Items = listDto
            };
        }

        /// <summary>
        /// 获取字典表（单条数据）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<GetKeyValuesOutput> GetKeyValues(Guid id)
        {
            var rst = await _db.Queryable<KeyValues>().Where(x => x.Id.Equals(id)).SingleAsync();
            var listDto = ObjectMapper.Map<KeyValues, GetKeyValuesOutput>(rst);
            return listDto;
        }

        /// <summary>
        /// 更新培训区域排序
        /// </summary>
        /// <param name="list">集合</param>
        public async Task<bool> UpdateSort(List<KeyValues> list)
        {
            return await _db.Updateable(list).WhereColumns(x => x.Id).UpdateColumns(t => new { t.Sort }).ExecuteCommandAsync() > 0 ? true : false;
        }

        /// <summary>
        /// 获取数据字典信息
        /// </summary>
        /// <param name="typeCode"></param>
        /// <returns></returns>
        public async Task<List<DictionaryOutput>> GetDictionary(string typeCode)
        {
            var list = await _dbPlatform.Queryable<Dictionaries>().Where(x => x.TypeCode == typeCode).OrderBy(x => x.OrderNum)
                .Select(x => new DictionaryOutput()
                {
                    DictionaryId = x.DictionaryId,
                    Code = x.Code,
                    Name = x.Name
                }).ToListAsync();
            return list;
        }
        /// <summary>
        /// 获取省份城市数据
        /// </summary>
        /// <param name="provinceName">省</param>
        /// <returns></returns>
        public async Task<List<CityOutput>> GetCityData(GetCityInput input)
        {
            var list = await _db.Queryable<City>()
                .WhereIF(!string.IsNullOrEmpty(input.ProvinceName), x => x.ProvinceName.Contains(input.ProvinceName))
                .WhereIF(!string.IsNullOrEmpty(input.CityName), x => x.CityName.Contains(input.CityName))
                .WhereIF(!string.IsNullOrEmpty(input.CountyName), x => x.CountyName.Contains(input.CountyName))
                .Distinct()
                .OrderBy(x => x.ProvinceName).OrderBy(x=>x.CityName )
                .ToListAsync();
            return list.Adapt<List<CityOutput>>();
        }
    }
}
